﻿/*

Copyright (c) 2009, Stephen Remde
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY STEPHEN REMDE ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL STEPHEN REMDE BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Graphs;
using System.Linq;

namespace ConceptLink
{
    class SearchContextParetoExplorer : SearchContext
    {

        public List<CandidateParato> open = new List<CandidateParato>();


        public SearchContextParetoExplorer(Article from, Article to, Graph g) 
            : this(from, to)
        {

            Graph = g;

            Node n = new WikiVisNode()
            {
                text = From.title,
                fixedinplace = true,       
                id = from.id
            };

            Relatedness.TryGet(from.title, to.title, out (n as WikiVisNode).h);

            Graph.AddNode(n);
        }

        public SearchContextParetoExplorer(Article from, Article to)
        {
            this.From = from;
            this.To = to;

            open.Add(new CandidateParato() { article = from, a = 1 });
            open.Add(new CandidateParato() { article = to, b = 1 });           
        }


        public override void Step(SearchContext.StepComplete callback)
        {

            var c = GetNextCandidate(open);

            //Links.Get(at.article.id, true,true,To.id,

            //    delegate(int idforget, Links.LinksResult[] outsresults, Links.LinksResult[] insresults)
            
            
        }


        CandidateParato GetNextCandidate(List<CandidateParato> arts)
        {

            var left = arts.AsEnumerable();

            while (left.Count() != 0)
            {
                var parato = GetParatoSet(left);

                var bestInSet = GetBestSpreadNotVisited(left);

                if (bestInSet != null)
                {
                    return bestInSet;
                }
                else
                {
                    left = left.Except(parato);
                }
            }

            return null;

        }

        private CandidateParato GetBestSpreadNotVisited(IEnumerable<CandidateParato> left)
        {
            var notvisited = left.Where(a => !a.visited);

            var vcount = notvisited.Count();

            if (vcount == 0)
            {
                //visited all in this set
                return null;
            }

            Random r = new Random();

            var i = r.Next(0, vcount);

            return notvisited.Skip(i).First();
        }

        private CandidateParato[] GetParatoSet(IEnumerable<CandidateParato> left)
        {
            List<CandidateParato> ret = new List<CandidateParato>();

            foreach (var t in left)
            {
                bool isUndominated = true;

                foreach (var u in left)                
                    if (u.a > t.a && u.b > t.b)
                    {
                        isUndominated = false;
                        break;
                    }

                if (isUndominated)                
                    ret.Add(t);             

            }

            return ret.ToArray();
        }


        public class CandidateParato
        {
            public Article article;

            public float a;
            public float b;
            public bool visited;
        }
    }
}
